
import 'package:dio/dio.dart';
import 'package:dustbin_sort/model/select_res_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class SelectPage extends StatefulWidget
{
	@override
	State<StatefulWidget> createState()=>_SelectPageState();

}
class _SelectPageState extends State<SelectPage>
{
	String formVal = "";
	List<Widget> list = [
		Container(
			margin: EdgeInsets.all(10),
			height: 150,
			child: Column(
				children: <Widget>[
					Expanded(
						child: Container(
							padding: EdgeInsets.only(left: 5),
							child: Text("搜索历史"),
							alignment: Alignment.centerLeft,
						),
						flex: 1,
					),
					Expanded(
						child: Wrap(
							spacing: 5, //主轴上子控件的间距
							runSpacing: 10, //交叉轴上子控件之间的间距
							children: [
								Container(
									child: Text("搜索历史"),
									padding: EdgeInsets.all(6),
									decoration: BoxDecoration(
											borderRadius: BorderRadius.circular(14),
											color: Color.fromARGB(255, 240, 240, 240)
									),
								),
								Container(
									child: Text("搜索历史"),
									padding: EdgeInsets.all(5),
									decoration: BoxDecoration(
											borderRadius: BorderRadius.circular(14),
											color: Color.fromARGB(255, 240, 240, 240)
									),
								),
								Container(
									child: Text("搜索历史"),
									padding: EdgeInsets.all(5),
									decoration: BoxDecoration(
											borderRadius: BorderRadius.circular(14),
											color: Color.fromARGB(255, 240, 240, 240)
									),
								),
								Container(
									child: Text("搜索历史"),
									padding: EdgeInsets.all(5),
									decoration: BoxDecoration(
											borderRadius: BorderRadius.circular(14),
											color: Color.fromARGB(255, 240, 240, 240)
									),
								),
								Container(
									child: Text("搜索历史"),
									padding: EdgeInsets.all(5),
									decoration: BoxDecoration(
											borderRadius: BorderRadius.circular(14),
											color: Color.fromARGB(255, 240, 240, 240)
									),
								),
								Container(
									child: Text("搜索历史"),
									padding: EdgeInsets.all(5),
									decoration: BoxDecoration(
											borderRadius: BorderRadius.circular(14),
											color: Color.fromARGB(255, 240, 240, 240)
									),
								),
							], //要显示的子控件集合
						),
						flex: 3,
					),
				],
			),
		),
		Container(
			margin: EdgeInsets.all(10),
			height: 80,
			child: Column(
				children: <Widget>[
					Expanded(
						child: Container(
							padding: EdgeInsets.only(left: 5),
							child: Text("热门搜索"),
							alignment: Alignment.centerLeft,
						),
						flex: 1,
					),
					Expanded(

						child: Row(
							children: <Widget>[
								Expanded(
									child: Row(
										children: <Widget>[
											Text("1. ",style: TextStyle(color: Colors.blue, fontWeight: FontWeight.w600),),Text("苹果皮"),
										],
									),
									flex: 1,
								),
								Expanded(
									child: Row(
										children: <Widget>[
											Text("2. ",style: TextStyle(color: Colors.blue, fontWeight: FontWeight.w600),),Text("啤酒瓶"),
										],
									),
									flex: 1,
								),
								Expanded(
									child: Row(
										children: <Widget>[
											Text("3. ",style: TextStyle(color: Colors.blue, fontWeight: FontWeight.w600),),Text("塑料袋"),
										],
									),
									flex: 1,
								),

							],
						),
						flex: 1,
					),
				],
			),
		),

	];
	void showToast(
			String text, {
				gravity: ToastGravity.CENTER,
				toastLength: Toast.LENGTH_SHORT,
			}) {
		Fluttertoast.showToast(
			msg: text,
			toastLength: Toast.LENGTH_SHORT,
			gravity: ToastGravity.BOTTOM,
			timeInSecForIos: 1,
			backgroundColor: Colors.grey[600], // 灰色背景
			fontSize: 16.0,
		);
	}
	Future<void> _selectItems() async {
		if(formVal == "")
			{
				return ;
			}

		try{
			Dio dio = new Dio();

			String SELECT_URL = "http://127.0.0.1:8888/test/select";
			Response response = await dio.post(SELECT_URL, data: {
				'pattern':formVal,
				'pageNum':1,
				'pageSize': 10
			});
			print(response.data);
			SelectResEntity selectResEntity = SelectResEntity().fromJson(response.data);
			if(selectResEntity.xList.length == 0)
				{
					print("啥都没有");
					showToast("什么都没有搜索到哦");
					return;
				}
			list = [];
			for(var item in selectResEntity.xList)
				{
					String type_info = "";
					if(item.type == 1){
						type_info = "可回收垃圾";
					}else if(item.type ==2){
						type_info = "干垃圾";
					}else if(item.type == 3){
						type_info = "湿垃圾";
					}else if(item.type == 4){
						type_info = "有害垃圾";
					}

					list.add(Container(
						decoration: BoxDecoration(
							color: Colors.white,
							borderRadius: BorderRadius.circular(10),
							border: new Border.all(style: BorderStyle.solid, color: Colors.black12),
						),
						margin: EdgeInsets.all(10),
						padding: EdgeInsets.all(10),
						child: Row(children: <Widget>[
							Text("("+type_info+")",style: TextStyle(color: Colors.grey),),
							Text(item.name),

						],),
					)
					);
				}
			setState(() {
			});
			return null;
		}on DioError catch(e){
			print(e.response.statusCode);

		}


	}

	_return(){
		Navigator.pop(context);
	}
	@override
	Widget build(BuildContext context) {
		// TODO: implement build
		return Scaffold(
				body: Stack(
						children: <Widget>[
							ListView(
								children: <Widget>[
									Container(
										margin: EdgeInsets.all(10),
										height: 80,
//										color: Colors.pink,
										child: Row(
											children: <Widget>[
												Expanded(
													child: Container(
														padding: EdgeInsets.only(left: 10),
														decoration: BoxDecoration(
															borderRadius: BorderRadius.circular(20),
															color: Colors.white,
															border: new Border.all(style: BorderStyle.solid, color: Colors.black12),

														),

														child: TextField(
															cursorColor: Colors.blue,
															onSubmitted: (String val) async {
																print("submit: "+ val);
																_selectItems();
															},
															onChanged:(String val){
																setState(() {
																  formVal = val;
																});
															},
															decoration: InputDecoration(
																enabledBorder: UnderlineInputBorder(
																	borderSide: BorderSide(color: Colors.transparent),
																),
																suffixIcon: GestureDetector(
																	child: Icon(Icons.search),
																	onTap: () async {
																		print("tap the search button");
																		_selectItems();
																	},
																),
																focusedBorder: UnderlineInputBorder(
																	borderSide: BorderSide(color: Colors.transparent),
																),
															),
														),
													),
													flex: 33,
												),
												Expanded(
													child: Container(
														child: MaterialButton(
															child: Container(
																margin: EdgeInsets.only(right: 0),
																child: Text("取消",style: TextStyle(
																		fontSize: 18,
																		color: Colors.black,
																		fontWeight: FontWeight.w300
																),),
															),
															onPressed: (){
																_return();
															},
														),
													),
													flex: 8,
												),
											],
										),
									),
									Column(
									children: list,
									),


								],
							)
						]
				)
		);

	}



}